/**
 * Created by weng on 2017/9/6.
 */
var schedule = require("node-schedule");
var sms = require("../common/sms");
var db = require('../common/dataAccess');
var moment = require('moment');
var job;
var initOrder = function (cb) {
    console.log("发动定时器");
    var obj = {};
    obj.commands = "select * from setordertime;";
    db.runSqlInfo(obj,function(setordertimeinfo,err){
        console.log("serordertimeinfo",setordertimeinfo);
        if( setordertimeinfo[0].flag == 0){
            return cb({code:200,info:"成功"});
        }else {
            cb({code:200,info:"成功"});
        }
        var time = "0 0 " + setordertimeinfo[0].hour + " * * *";
        job = schedule.scheduleJob(time, function(){
            console.log("在这个时间发送短信");
            var time = moment().add(1,'day').format("YYYY-MM-DD");
            var obj = {};
            obj.commands = "select * from ordertime where status = 0 and time = '" + time  + "'";
            db.runSqlInfo(obj,function(info,err){
                console.log("info is:",info);
                if(err){
                     console.log({code:500,info:"获取信息错误"});
                    return
                }

                else
                {
                    var phone = setordertimeinfo[0].phone;

                    var setInfo ={};
				   for(var i in info){
                        if(!setInfo[info[i]["orderTime"]]){
                            setInfo[info[i]["orderTime"]] = [info[i]["phone"] + "预约地点：" +info[i]["address"] + ",人数：" +　info[i]["num"] ];
                        }else {
                            setInfo[info[i]["orderTime"]].push(info[i]["phone"] + "预约地点：" + info[i]["address"]+ ",人数：" +　info[i]["num"] );
                        }
                    }
                    console.log("setInfo",setInfo);

                    for(var i in setInfo){
                        var sentTimes = time + " " + i;
                        var numbers = "";
                        for(var j in setInfo[i]){
                            numbers += setInfo[i][j] + ",";
                        }
                        numbers = numbers.substring(0,numbers.length - 1);
                        var msgInfo = JSON.stringify({number:numbers,time:sentTimes});
                        console.log("msgInfo",msgInfo);

                        sentMsg(phone,msgInfo)
                    }
                   // return cb({code:200,info:"成功"});
                }
            })
        });

    });
};

var updateNotice = function (parame,req,cb) {
    console.log("parme",parame);
    console.log("job",job);
    var value = parame.value;
    var type = parame.type;
    var flag = parame.flag;

    if(flag == 1){
        job.cancel();
    }

    var obj ={};
    switch (type){
        case 'flag':
            obj.commands = "update setordertime set flag =  '" + value + "'";
            break;
        case 'phone':
            obj.commands = "update setordertime set phone =  '" + value + "'";
            break;
        case 'hour':
            obj.commands = "update setordertime set hour =  '" + value + "'";
            break;
        default:
            return cb({code:500,info:'修改失败'})
            return;

    }

    db.runSqlInfo(obj,function(setordertimeinfo,errinfo) {
        initOrder(function (info) {
            console.log("info is",info)
            if(info.code == 200){
                return cb({code:200,info:'修改成功'})
            }else {
                return cb({code:500,info:'修改失败'})
            }
        });
    })
};

var sentMsg = function (phone,msgInfo) {
    sms.sendMessage(phone, 'SMS_100755008',  msgInfo, function (err,info) {
        console.log("短信发送日志",err,info);
        if(err){
            console.error("短信发送日志失败",err);
        }
    });
}




exports.initOrder = initOrder;
exports.updateNotice = updateNotice;